<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>比较大小-watch实现</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>

    <!-- 

    -->

    <!-- 准备一个容器 -->
    <div id="app">
        <h1>{{msg}}</h1>
        数字1：<input type='text' v-model="num1" /> <br />
        数字2：<input type='text' v-model="num2" /> <br />
        比较大小：{{compareResult}}

    </div>

    <script>

        const vm = new Vue({
            el: '#app',
            data: {
                msg: '比较大小-watch实现',
                num1: 0,
                num2: 0,
                compareResult: ''
            },
            watch: {
                num1: {
                    immediate: true,
                    // 当handler只有一个属性的时候，值为新的值
                    handler(newVal) {
                        let result = newVal - this.num2
                        if (result == 0) {
                            this.compareResult = newVal + '=' + this.num2
                        } else if (result > 0) {
                            this.compareResult = newVal + '>' + this.num2
                        } else {
                            this.compareResult = newVal + '<' + this.num2
                        }
                    }
                },
                num2: {
                    immediate: true,
                    // 当handler只有一个属性的时候，值为新的值
                    handler(newVal) {
                        let result = this.num1 - newVal
                        if (result == 0) {
                            this.compareResult = this.num1 + '=' + newVal
                        } else if (result > 0) {
                            this.compareResult = this.num1 + '>' + newVal
                        } else {
                            this.compareResult = this.num1 + '<' + newVal
                        }
                    }
                }
            }
        })
    </script>


</body>


</html>